package com.lmiot.xyclick.KeYi;

import android.text.TextUtils;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AesUtilHttp {
    private static final String AesKey = "xiaoyipro123456";
    private static final String Algorithm = "DESede";
    private static final String signKey = "xypro2021";

    public static String set3DesString(String str) {
        try {
            if (!TextUtils.isEmpty(str)) {
                return Base64.encodeToString(encryptMode(AesKey, str.getBytes("UTF-8")), 0);
            }
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String get3DesString(String str) {
        try {
            if (!TextUtils.isEmpty(str)) {
                return new String(decryptMode(AesKey, Base64.decode(str, 0)), "UTF-8");
            }
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private static byte[] encryptMode(String str, byte[] bArr) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(build3DesKey(str), Algorithm);
            Cipher instance = Cipher.getInstance(Algorithm);
            instance.init(1, secretKeySpec);
            return instance.doFinal(bArr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static byte[] decryptMode(String str, byte[] bArr) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(build3DesKey(str), Algorithm);
            Cipher instance = Cipher.getInstance(Algorithm);
            instance.init(2, secretKeySpec);
            return instance.doFinal(bArr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static byte[] build3DesKey(String str) throws UnsupportedEncodingException {
        byte[] bArr = new byte[24];
        byte[] bytes = str.getBytes("UTF-8");
        if (24 > bytes.length) {
            System.arraycopy(bytes, 0, bArr, 0, bytes.length);
        } else {
            System.arraycopy(bytes, 0, bArr, 0, 24);
        }
        return bArr;
    }

    public static String getSign(int i, String str, String str2) {
        String strToMD5 = strToMD5(i + str + str2);
        return strToMD5(strToMD5 + signKey);
    }

    public static String strToMD5(String str) {
        String str2 = new String();
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.update(str.getBytes());
            byte[] digest = instance.digest();
            StringBuffer stringBuffer = new StringBuffer("");
            for (byte b : digest) {
                int i = b;
                if (b < 0) {
                    i = b + 256;
                }
                if (i < 16) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(Integer.toHexString(i == 1 ? 1 : 0));
            }
            return stringBuffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return str2;
        }
    }
}
